In [1]:
%autosave 120
import numpy as np
import matplotlib.pyplot as pl
from numba import jit


Autosaving every 120 seconds

In [8]:
from scipy.signal import gaussian
SAMPLES = 10

# create the test data instance
xs = np.random.randn(SAMPLES, 1000)
y = gaussian(50, 5.0)
pl.plot(y)


Out[8]:
[<matplotlib.lines.Line2D at 0x10954f110>]

Convolution defined by numpy: $y \ast x[n] = \sum_m y[m] \, x[n - m]$


In [9]:
def convolve_np(y, xs):
    return [np.convolve(y, x, mode='valid') for x in xs]

In [10]:
%timeit -n10 convolve_np(y, xs)
zs_np = convolve_np(y, xs)
pl.plot(xs[0][len(y) - 1:])
pl.plot(zs_np[0])


10 loops, best of 3: 427 µs per loop
Out[10]:
[<matplotlib.lines.Line2D at 0x101c7d1d0>]

In [30]:
from itertools import izip

def convolve_py(y, xs):
    zs = np.zeros((xs.shape[0], xs.shape[1] - len(y) + 1), dtype=xs.dtype)
    for x, z in izip(xs, zs):
        for n in xrange(len(y) - 1, len(x)):
            for m in xrange(len(y)):
                z[n - len(y) + 1] += x[n - m] * y[m]
    return zs

%timeit -n10 convolve_py(y, xs)
zs_py = convolve_py(y, xs)
pl.plot(zs_np[0])
pl.plot(zs_py[0])


10 loops, best of 3: 366 ms per loop
Out[30]:
[<matplotlib.lines.Line2D at 0x100f4ec10>]

In [34]:
@jit(nopython=True)
def convolve_nb(y, xs):
    zs = list()
    for i in range(len(xs)):
        z = np.zeros(xs.shape[1] - len(y) + 1, dtype=xs.dtype)
        for n in xrange(len(y) - 1, len(xs[i])):
            for m in xrange(len(y)):
                z[i, n - len(y) + 1] += xs[i, n - m] * y[m]
        zs.append(z)
    return zs

zs_nb = convolve_nb(y, xs)
%timeit -n10 convolve_nb(y, xs)
pl.plot(zs_nb[0])
pl.plot(zs_py[0])


---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-34-4b16235bf9c6> in <module>()
     10     return zs
     11 
---> 12 zs_nb = convolve_nb(y, xs)
     13 get_ipython().magic(u'timeit -n10 convolve_nb(y, xs)')
     14 pl.plot(zs_nb[0])

/usr/local/lib/python2.7/site-packages/numba/dispatcher.pyc in _compile_for_args(self, *args, **kws)
    157         assert not kws
    158         sig = tuple([self.typeof_pyval(a) for a in args])
--> 159         return self.compile(sig)
    160 
    161     def inspect_llvm(self, signature=None):

/usr/local/lib/python2.7/site-packages/numba/dispatcher.pyc in compile(self, sig)
    320                                           self.py_func,
    321                                           args=args, return_type=return_type,
--> 322                                           flags=flags, locals=self.locals)
    323 
    324             # Check typing error if object mode is used

/usr/local/lib/python2.7/site-packages/numba/compiler.pyc in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
    592     pipeline = Pipeline(typingctx, targetctx, library,
    593                         args, return_type, flags, locals)
--> 594     return pipeline.compile_extra(func)
    595 
    596 

/usr/local/lib/python2.7/site-packages/numba/compiler.pyc in compile_extra(self, func)
    315                 raise e
    316 
--> 317         return self.compile_bytecode(bc, func_attr=self.func_attr)
    318 
    319     def compile_bytecode(self, bc, lifted=(), lifted_from=None,

/usr/local/lib/python2.7/site-packages/numba/compiler.pyc in compile_bytecode(self, bc, lifted, lifted_from, func_attr)
    324         self.lifted_from = lifted_from
    325         self.func_attr = func_attr
--> 326         return self._compile_bytecode()
    327 
    328     def compile_internal(self, bc, func_attr=DEFAULT_FUNCTION_ATTRIBUTES):

/usr/local/lib/python2.7/site-packages/numba/compiler.pyc in _compile_bytecode(self)
    579 
    580         pm.finalize()
--> 581         return pm.run(self.status)
    582 
    583 

/usr/local/lib/python2.7/site-packages/numba/compiler.pyc in run(self, status)
    207                     # No more fallback pipelines?
    208                     if is_final_pipeline:
--> 209                         raise patched_exception
    210                     # Go to next fallback pipeline
    211                     else:

TypingError: Caused By:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 201, in run
    res = stage()
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 414, in stage_nopython_frontend
    self.locals)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 708, in type_inference_stage
    infer.build_constrain()
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 386, in build_constrain
    self.constrain_statement(inst)
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 506, in constrain_statement
    self.typeof_assign(inst)
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 538, in typeof_assign
    self.typeof_global(inst, inst.target, value)
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 603, in typeof_global
    loc=inst.loc)
TypingError: Untyped global name 'list'
File "<ipython-input-34-4b16235bf9c6>", line 3

Failed at nopython (nopython frontend)
Untyped global name 'list'
File "<ipython-input-34-4b16235bf9c6>", line 3

In [44]:
@jit(nopython=True)
def restsum(xs):
    rval = np.zeros(xs.shape[0])
    for i in range(len(xs)):
        rval += sum(xs[i])

In [46]:
from numba import cuda

In [ ]: